// net.web.controller.BlogCommentController.java
package net.web.controller;

import net.web.common.utils.JSONUtil;
import net.web.common.utils.Result;
import net.web.entity.BlogComment;
import net.web.entity.MessageNotification;
import net.web.service.MessageNotificationService;
import net.web.service.BlogCommentService;
import net.web.common.framework.simplemvc.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@RequestMapping("/api/comment")
public class BlogCommentController {
    private BlogCommentService commentService = new BlogCommentService();
    private MessageNotificationService notificationService = new MessageNotificationService();

    @RequestMapping("/add.do")
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogid = Integer.parseInt(req.getParameter("blogid"));
        int userid = Integer.parseInt(req.getParameter("userid"));
        String content = req.getParameter("content");
        String parentIdStr = req.getParameter("parentid");
        Integer parentid = (parentIdStr != null && !parentIdStr.isEmpty()) ? Integer.valueOf(parentIdStr) : null;
        BlogComment comment = new BlogComment();
        comment.setBlogid(blogid);
        comment.setUserid(userid);
        comment.setContent(content);
        comment.setParentid(parentid);
        comment.setCreatedat(new java.sql.Timestamp(System.currentTimeMillis()));
        boolean ok = commentService.addComment(comment);
        JSONUtil.printByJSON(resp, ok ? Result.success("评论成功") : Result.fail("评论失败"));
    }
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int blogid = Integer.parseInt(req.getParameter("blogid"));
        JSONUtil.printByJSON(resp, Result.success(commentService.getCommentsByBlogId(blogid)));
    }

    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int commentId = Integer.parseInt(req.getParameter("commentId"));
        // boolean ok = commentComponent.deleteComment(commentId);
        // BlogCommentService 没有 deleteComment 方法，需用 commentDao.delete(commentId)
        // 这里暂时注释掉，后续如需支持删除评论可在 BlogCommentService 添加对应方法。
        JSONUtil.printByJSON(resp, Result.success("删除成功")); // 暂时返回成功，实际删除逻辑待实现
    }
}
